package app.sceledroid.activity;

import java.util.ArrayList;

import org.jsoup.nodes.Comment;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import app.sceledroid.R;
import app.sceledroid.adapter.ProfileAdapter;
import app.sceledroid.entity.Profile;
import app.sceledroid.network.SearchHandler;
import app.sceledroid.services.Event;

public class DirectEmailActivity extends ListActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = this.getIntent().getExtras();
		String key = bundle.getString("key");
		SearchHandler search = new SearchHandler();
		final ArrayList<Profile> result = search.searchProfile(key);
		String[] values = new String[result.size()];
		for (int i = 0; i < values.length; i++) {
			values[i] = "Name : " + result.get(i).getName() + "\n" + "Email : "
					+ result.get(i).getEmail();
		}
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, values));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				String email = result.get(position).getEmail();
				Toast.makeText(DirectEmailActivity.this, email, Toast.LENGTH_LONG).show();
				Intent mail = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"+email) );
				ComponentName cn = new ComponentName("com.android.mms", 
						"com.android.mms.ui.ComposeMessageActivity");
				mail.setComponent(cn);
				//mail.setData();
				startActivity(mail);
			}
		});
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.afterlogin_menu, menu);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//intent for settings
		Intent intent  = new Intent(DirectEmailActivity.this, SettingsActivity.class);
		//toast for others
		Context context = getApplicationContext();
		CharSequence help = "SceleDroid";
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(context, help, duration);
		
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.help:
	       // showHelp();
	    	toast.show();
	        return true;
	    case R.id.settings:
	    	startActivity(intent);
	    	return true;
	    case R.id.credits:
	    	toast.show();
	    	//credits here;
	    	return true;
	    case R.id.logout:
	    	intent = new Intent(DirectEmailActivity.this, LoginActivity.class);
	    	startActivity(intent);
	    	SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
			Editor e = pref.edit();//this.getPreferences(Context.MODE_PRIVATE).edit();
			e.clear();
			e.commit();

			intent = new Intent(this, LoginActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
	    	return true;
	    case R.id.search:
	    	intent = new Intent(DirectEmailActivity.this, SearchActivity.class);
	    	startActivity(intent);
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}
